package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.ChannelMapper;
import com.usian.admin.service.ChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/22 15:09
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper,AdChannel> implements ChannelService {

    @Override
    public ResponseResult findAdChannel(ChannelDto channelDto) {
        if(null==channelDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();
        QueryWrapper<AdChannel> adChannelQueryWrapper = new QueryWrapper<>();
        if(null!=channelDto.getName()){
            adChannelQueryWrapper.eq("name",channelDto.getName());
        }
        IPage<AdChannel> objectIPage = new Page<AdChannel>(channelDto.getPage(),channelDto.getSize());
        // adChannelQueryWrapper.lambda().eq(AdChannel::getName,channelDto.getName());
       // List<AdChannel> adChannels = this.baseMapper.selectList(adChannelQueryWrapper);
        IPage<AdChannel> page = this.page(objectIPage, adChannelQueryWrapper);
        ResponseResult responseResult =
                new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {

        if(null==adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if(null==adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        this.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        if(null==id){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel channel = this.baseMapper.selectById(id);
        if(null==channel){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        int i =  1/0;




        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
